//
//  SPtime.m
//  Sport
//
//  Created by Cass on 2018/1/27.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPtime.h"
#import "NSDate+SPExtension.h"
@implementation SPtime
+ (instancetype)sharedTime{
    static id _instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}
- (NSString *)timeWithCreatTime:(NSString *)time{
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.timeZone = [NSTimeZone timeZoneWithName:@"beijin"];
    fmt.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm";
    NSDate *creatDate = [fmt dateFromString:time];
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitSecond;
    //计算两个日期的差值
    NSDateComponents *cmps = [calendar components:unit fromDate:creatDate toDate:now options:0];
    if ([creatDate isThisYear]) {
        if ([creatDate isYesterday]) {
            fmt.dateFormat = @"昨天 HH:mm";
            return [fmt stringFromDate:creatDate];
        }else if ([creatDate isToday]){
            if (cmps.hour > 1) {
                return [NSString stringWithFormat:@"%ld小时前",(long)cmps.hour];
            }else if (cmps.minute >= 1){
                return [NSString stringWithFormat:@"%ld分钟前",(long)cmps.minute];
            }else{
                return @"刚刚";
            }
        }else{
            fmt.dateFormat = @"MM-dd HH:mm";
            return [fmt stringFromDate:creatDate];
        }
    }else{
        fmt.dateFormat = @"yyyy-MM-dd HH:mm";
        return [fmt stringFromDate:creatDate];
    }
    return time;
}
@end
